package org.softmed.rest.admin.httpMethods;

import java.awt.Dimension;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import main.RESTEditor;

import org.softmed.rest.admin.interfaces.CreateListener;
import org.softmed.rest.admin.interfaces.FileHandler;
import org.softmed.rest.admin.interfaces.PathChangedListener;
import org.softmed.rest.admin.interfaces.SelectionPathChanged;
import org.softmed.rest.admin.module.ValidationException;
import org.softmed.swing.IconManager;

import restlet.description.convert.RESTResourceDescription;


public class DeleteConfig extends JPanel implements FileHandler,SelectionPathChanged, CreateListener, PathChangedListener {

	SimpleDeleteConfig simpleDeleteConfig = new SimpleDeleteConfig();
	MethodActionPanel actionPanel = new MethodActionPanel();
	FileHandler fileHandler;
	PathChangedListener pathChangedListener;

	public DeleteConfig() {
		setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));

		setPreferredSize(new Dimension(540, 50));
		setMaximumSize(new Dimension(540, 50));
		setMinimumSize(new Dimension(540, 50));
		actionPanel.setFileHandler(this);
		actionPanel.setCreateListener(this);
		simpleDeleteConfig.setPathChangedListener(this);
		add(simpleDeleteConfig);
		add(actionPanel);
	}

	public void setResourceDescription(
			RESTResourceDescription resourceDescription) {
		simpleDeleteConfig.setResourceDescription(resourceDescription);

	}

	public void validateData() throws ValidationException {
		// if (delete.getText() == null || delete.getText().length() == 0) {
		// throw new ValidationException("Module Name can't be empty !!! ");
		// }
		simpleDeleteConfig.validateData();
	}

	public void selectionPathChanged(boolean directory, String name, String parent) {
		actionPanel.selectionPathChanged(directory, name, parent);

	}

	@Override
	public String retrieveFilePath() {
		String path = fileHandler.retrieveFilePath();
		if (path.endsWith(".class") || path.endsWith(".java")) {
			//use a FQCN instead of a file path
			int index = path.lastIndexOf('.');
			path = path.substring(1,index); 
			path = path.replace('/', '.');
		}

		simpleDeleteConfig.delete.setText(path);
		// TODO activate open File button on action Panel
		return null;
	}

	public FileHandler getFileHandler() {
		return fileHandler;
	}

	public void setFileHandler(FileHandler fileHandler) {
		this.fileHandler = fileHandler;
	}


	@Override
	public void created() {
		String path = RESTEditor.RESTEditor.createFileWithPopup();
		simpleDeleteConfig.delete.setText(path);
	}

	@Override
	public void pathChanged(String path) {
		actionPanel.pathChanged(path);
		pathChangedListener.pathChanged(path);
		
	}

	public PathChangedListener getPathChangedListener() {
		return pathChangedListener;
	}

	public void setPathChangedListener(PathChangedListener pathChangedListener) {
		this.pathChangedListener = pathChangedListener;
	}
}
